<template>
	<view>
		<cu-custom :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">签到</block>
		</cu-custom>
		<view style="height: 20upx;"></view>
		<view class="flex justify-between align-center padding">
			<view v-if="userInfo.day_sign==0" class="flex justify-start align-center">
				<image src="../../../static/images/setting/day_sign_not.png" style="width: 25px;height: 25px;"></image>
				<view style="width: 20upx;"></view>
				<button @tap="daySignAction" class="cu-btn bg-grey round">立即签到</button>
			</view>
			<view v-else class="flex justify-start align-center">
				<image src="../../../static/images/setting/index-sign.png" style="width: 25px;height: 25px;"></image>
				<view style="width: 20upx;"></view>
				<button class="cu-btn bg-green round">您已签到</button>
			</view>
			<text class="text-grey">每日签到完成后可抽奖一次</text>
		</view>
		<view style="height: 40upx;"></view>
		<view class="flex justify-center align-center" style="width: 100%;">
			<LotteryDraw @get_winingIndex='get_winingIndex'  @luck_draw_finish='luck_draw_finish'></LotteryDraw>
		</view>
	</view>
</template>

<script>
	import {
		daySign,
		daySignReward,
		getUserInfoDetail
	} from '@/api/api.js'
	import {
		getUserInfo,
		updateUserInfo
	} from '@/api/auth.js'
	import LotteryDraw from '@/components/SJ-LotteryDraw/SJ-LotteryDraw.vue';
	export default {
		components: {
			LotteryDraw
		},
		data() {
			return {
				userInfo:getUserInfo(),
				didSign:false,
				lottery_draw_param:{
					startIndex: 0, //开始抽奖位置，从0开始
					totalCount: 4, //一共要转的圈数
					winingIndex: 4, //中奖的位置，从0开始
					speed: 50 //抽奖动画的速度 [数字越大越慢,默认100]
				}
			}
		},
		onLoad() {
			getUserInfoDetail().then(res => {
				setUserInfo(res.data)
				this.userInfo = res.data
				uni.$emit('UserInfoBaseRefresh')
			}).catch(error => {
			})
		},
		methods: {
			daySignAction() 
			{
				uni.showLoading({
					title: '正在签到...'
				})
				daySign().then(res => {
					this.userInfo.day_sign = 1
					updateUserInfo()
					uni.showToast({
						title: '签到成功!'
					})
				}).catch(error => {
				
				})
			},
			// 修改获奖位置（可以在这里获取后台的数据
			get_winingIndex(callback){
				daySignReward().then(res=>{
					this.lottery_draw_param.winingIndex=res.data.index;
					this.lottery_draw_param.winingScore = res.data.score
					//props修改在小程序和APP端不成功，所以在这里使用回调函数传参，
					callback(this.lottery_draw_param);
				}).catch(error=>{
					
				})
			},
			// 抽奖完成
			luck_draw_finish(param){
				
				console.log(param)
				// console.log(`抽到第${param+1}个方格的奖品`)
				uni.showModal({
					title:'抽奖结果',
					content:'恭喜你，今日签到抽中'+this.lottery_draw_param.winingScore+'个山竹!',
					confirmText:'逛逛去咯'
				})
			}
		},
	}
</script>

<style scoped>

</style>
